0745. 前缀和后缀搜索【困难】
1. 📝 题目描述
设计一个包含一些单词的特殊词典,并能够通过前缀和后缀来检索单词。
实现 WordFilter 类:
WordFilter(string[] words)使用词典中的单词words初始化对象。f(string pref, string suff)返回词典中具有前缀pref和后缀suff的单词的下标。如果存在不止一个满足要求的下标,返回其中 最大的下标。如果不存在这样的单词,返回-1。
示例:
txt
输入
["WordFilter", "f"]
[[["apple"]], ["a", "e"]]
输出
[null, 0]
解释
WordFilter wordFilter = new WordFilter(["apple"]);
wordFilter.f("a", "e"); // 返回 0,因为下标为 0 的单词:前缀 prefix = "a" 且 后缀 suffix = "e"。1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
提示:
1 <= words.length <= 10^41 <= words[i].length <= 71 <= pref.length, suff.length <= 7words[i]、pref和suff仅由小写英文字母组成- 最多对函数
f执行10^4次调用
2. 🎯 s.1 - 解法 1
js
// todo1
- 时间复杂度:
- 空间复杂度: